package com.example.maze;

import java.util.ArrayList;

class Maze {
   //[ ========== static part ==========
   final static int WEST = 0,
                    SOUTH= 1,
                    EAST = 2,
                    NORTH= 3;                                  
   //] ========== static part ==========
   
   static ArrayList<Edge> spanningTree= new ArrayList<Edge>();
   static int             M,N,SIZE;
   static Cell[][]        mazeCells;
   
   public static void setData(int _M,
                              int _N,
                              int _CELL_SIZE,
                              ArrayList<Edge> ST) {
      M= _M;
      N= _N;
      SIZE= _CELL_SIZE;
      //[ init mazeCells
      mazeCells= new Cell[M][N];
      for(int i=0; i<M; i++) {
         for(int j=0; j<N; j++) {
            mazeCells[i][j]= new Cell(i,j,SIZE);
         }
      }
      for(int i=0; i<M; i++) {
         mazeCells[i][0].borders[WEST]  = true;
         mazeCells[i][N-1].borders[EAST]= true;
      }
      for(int i=0; i<N; i++) {
         mazeCells[M-1][i].borders[SOUTH]= true;         
         mazeCells[0][i].borders[NORTH]  = true;
      }
//      for(int i=0; i<M; i++) {
//         for(int j=0; j<N; j++) {
//            mazeCells[i][j].distance   = DataSet.VERTEXES[i][j].distance;
//            mazeCells[i][j].colorValue = DataSet.VERTEXES[i][j].colorValue;
//         }
//      }
      mazeCells[0][0].type= Cell.ENTRY;
      mazeCells[M-1][N-1].type= Cell.EXIT;
      //]
      spanningTree.clear();
      for(Edge e: ST) spanningTree.add(e);                    // �ۤ@��, �קK�������
   }
   
   public static Cell[][] getMaze() {
      int v1PosM, v1PosN, v2PosM, v2PosN;
      int direction=0;
      for(Edge e : spanningTree) {
         v1PosM= e.v1.posM;
         v1PosN= e.v1.posN;
         v2PosM= e.v2.posM;
         v2PosN= e.v2.posN;
         
         if(v1PosM>v2PosM) {       // To check the direction
            direction= NORTH;
         } else if(v1PosM<v2PosM) {
            direction= SOUTH;
         } else if(v1PosN>v2PosN) {
            direction= WEST;
         } else if(v1PosN<v2PosN){
            direction= EAST;
         }
         
         switch (direction) {      // knock down the wall between v1 and v2
            case WEST : mazeCells[v1PosM][v1PosN].walls[WEST] = false;
                        mazeCells[v2PosM][v2PosN].walls[EAST] = false;
                        break;
            case SOUTH: mazeCells[v1PosM][v1PosN].walls[SOUTH]= false;
                        mazeCells[v2PosM][v2PosN].walls[NORTH]= false;
                        break;
            case EAST : mazeCells[v1PosM][v1PosN].walls[EAST] = false;
                        mazeCells[v2PosM][v2PosN].walls[WEST] = false;
                        break;
            case NORTH: mazeCells[v1PosM][v1PosN].walls[NORTH]= false;
                        mazeCells[v2PosM][v2PosN].walls[SOUTH]= false;                      
                        break;
         }
//D      System.out.println("("+v1PosM+v1PosN+")�P("+v2PosM+v2PosN+")�������𥴱��F");
//D      System.out.println("��V�O "+direction);         
      }
      mazeCells[0][0].type    = Cell.ENTRY;
      mazeCells[M-1][N-1].type= Cell.EXIT;
      return mazeCells;
   }
}